package com.tfy.liquor.controller;

import com.tfy.liquor.bean.CategoryInfo;
import com.tfy.liquor.bean.Result;
import com.tfy.liquor.service.CategoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 产品类别信息
 */
@RestController
@RequestMapping("/categoryInfo")
public class CategoryInfoController {
    @Autowired
    private CategoryInfoService categoryInfoService;

    @RequestMapping("/addCategoryInfo")
    public Result addCategoryInfo(CategoryInfo categoryInfo) {
        if (null == categoryInfo || null == categoryInfo.getCategoryName()) {
            // 返回参数错误
            return new Result().bad_CodeIsNull();
        }
        if (null == categoryInfo.getParentId()) {
            categoryInfo.setParentId(0);
        }
        if (categoryInfoService.addCategoryInfo(categoryInfo)) {
            return new Result().ok("添加成功！");
        }
        return new Result().error("添加失败！");
    }

    /**
     * 获取类别信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/getCategoryInfo")
    public Result getCategoryInfo(Integer pageNum, Integer pageSize, String categoryName) {
        if (null == pageNum || null == pageSize) {
            // 返回参数错误
            return new Result().bad_CodeIsNull();
        }
        if (null == categoryName || "".equals(categoryName.trim())) {
            categoryName = null;
        }
        return new Result(categoryInfoService.getAllCategoryInfo(pageNum, pageSize, categoryName)).ok();
    }

    /**
     * 获取全部类别信息
     *
     * @return
     */
    @RequestMapping("/getAllCategoryInfo")
    public Result getAllCategoryInfo() {
        return categoryInfoService.getAllCategoryInfo();
    }

    /**
     * 修改类别信息
     *
     * @return
     */
    @RequestMapping("/editCategoryInfo")
    public Result editCategoryInfo(CategoryInfo categoryInfo) {
        if (null == categoryInfo || null == categoryInfo.getCategoryId() || null == categoryInfo.getCategoryName()) {
            // 返回参数错误
            return new Result().bad_CodeIsNull();
        }
        if (categoryInfoService.editCategoryInfo(categoryInfo)) {
            return new Result().ok("修改成功！");
        }
        return new Result().error("修改失败！");
    }

    /**
     * 停用类别信息
     *
     * @param categoryInfo
     * @return
     */
    @RequestMapping("/stopCategoryInfo")
    public Result stopCategoryInfo(CategoryInfo categoryInfo) {
        if (null == categoryInfo || null == categoryInfo.getCategoryId()) {
            // 返回参数错误
            return new Result().bad_CodeIsNull();
        }
        if (categoryInfoService.stopCategoryInfo(categoryInfo)) {
            return new Result().ok("停用成功！");
        }
        return new Result().error("停用失败！");
    }

    /**
     * 启用类别信息
     *
     * @param categoryInfo
     * @return
     */
    @RequestMapping("/enableCategoryInfo")
    public Result enableCategoryInfo(CategoryInfo categoryInfo) {
        if (null == categoryInfo || null == categoryInfo.getCategoryId()) {
            // 返回参数错误
            return new Result().bad_CodeIsNull();
        }
        if (categoryInfoService.enableCategoryInfo(categoryInfo)) {
            return new Result().ok("启用成功！");
        }
        return new Result().error("启用失败！");
    }
}
